using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Facebook.Desktop;
using Facebook.Service;
using WindowsLive.Writer.Api;


// Facebook Photos Plugin for Windows Live Writer
// Utilizes the Facebook.NET API to login and retrieve photos from Facebook and insert them into a blog post.
//

namespace FacebookPhotosLiveWriterPlugin
{
    [WriterPluginAttribute
      ("be2d7714-6c02-441d-83e6-63380ec20068", "Facebook Photos Plugin", ImagePath = "Images.Facebook.png",
            HasEditableOptions = false, PublisherUrl = "http://blog.varchev.net/", 
            Description = "Insert photos from Facebook into your blog post.")]
    [InsertableContentSourceAttribute("Facebook Photos")]
    public class FacebookPlugin : ContentSource
    {
        internal static FacebookService Service;

        public override DialogResult CreateContent(System.Windows.Forms.IWin32Window dialogOwner, ref string newContent)
        {
            try
            {
                List<Photo> result = this.GetContent(dialogOwner);

                if (result != null)
                    newContent = this.FormatOutput(result);

                return DialogResult.OK;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return DialogResult.OK;
            }
        }

        /// <summary>
        /// Format the output form Photos into an HTML string that is embedded 
        /// into the LiveWriter document.
        /// </summary>
        /// <param name="result">A list of photos that have to be embedded</param>
        /// <returns>HTML of the embedded image</returns>
        private string FormatOutput(List<Photo> result)
        {
            StringBuilder html = new StringBuilder();
            foreach (Photo photo in result)
            {
                html.AppendFormat("<img src=\"{0}\" alt=\"{1}\" /> ", photo.LargeImageUrl, photo.Caption);
            }
            return html.ToString();
        }


        /// <summary>
        /// Wrapper function that pops up the WPF Windows that
        /// displays the Facebook Photo List.
        /// </summary>
        /// <param name="owner"></param>
        /// <returns></returns>
        private List<Photo> GetContent(IWin32Window owner)
        {
            Form host = new Form();

            FacebookClientSession session =
               new FacebookClientSession("041c618f9104c2ce28f83bb7734ac9be", "ae4b6e3b0090dcd0aaacfa50a6d50aeb");

            if (session.Initialize(host))
            {
                Service = new FacebookService(session);


                PhotoAlbumsForm search = new PhotoAlbumsForm();

                System.Windows.Interop.WindowInteropHelper helper = new System.Windows.Interop.WindowInteropHelper(search);
                helper.Owner = owner.Handle;

                if (!search.ShowDialog().Value)
                    return null;

                return search.SelectedPhotos;
            }

            return null;
        }
    }
}
